package com.gaoyi.crm.vm;

import android.app.Application;

import androidx.annotation.NonNull;

import com.gaoyi.crm.R;
import com.gaoyi.crm.apiresult.ApiResultData;
import com.gaoyi.crm.apiresult.PagedList;
import com.gaoyi.crm.callback.OnResultListener;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.db.dto.PmnDto;
import com.gaoyi.crm.repository.PmnRepository;
import com.gaoyi.crm.util.ToastUtil;
import com.gaoyi.crm.vm.base.BaseListViewModel;

import java.util.List;

public class HomePageViewModel extends BaseListViewModel<PmnDto> {

    private final PmnRepository mRepository;

    public HomePageViewModel(@NonNull Application application) {
        super(application);
        mRepository = new PmnRepository();
    }

    public void getPmns(int limit) {
        mRepository.getPmnByModifyAt(limit, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                ApiResultData<PagedList<PmnDto>> apiResultData = (ApiResultData<PagedList<PmnDto>>) apiResult;
                int status = apiResultData.getStatus();
                mResultCode.setValue(status);
                if (status != ApiStatus.SUCCESS) {
                    ToastUtil.showToast(apiResultData.getMessage());
                    return;
                }
                List<PmnDto> data = apiResultData.getData().getList();
                if (data != null && !data.isEmpty()) {
                    addListValue(data);
                }
            }

            @Override
            public void onFails(Exception e) {
                mResultCode.setValue(ApiStatus.NET_ERROR);
                ToastUtil.showToast(R.string.network_error);
            }
        });
    }
}
